home *** CD-ROM | disk | FTP | other *** search
- /*
- * Source - DrawMyStuff.c
- * based on Mark B. Kauffman's article
- * in the 9/90 MacTutor
- *
- * revised to demonstrate polymorphism
- * -- the way a subclass can override
- * its ancestor’s methods
- */
-
- #include "oops.h"
- #include "CShape.h"
-
- void DrawMyStuff()
- {
- int i;
-
- /* set up a big generic array */
- CShape *myShape[9];
-
- /* let’s create 4 rectangles... */
- for (i=0; i<4; i++)
- myShape[i] = new(CRectangle);
- /* ... one line... */
- myShape[4] = new(CLine);
- /* ... and 4 ovals */
- for (i=5; i<9; i++)
- myShape[i] = new(COval);
-
- /* place the rectangles */
- myShape[0]->SetShapeLoc(200,100,300,200);
- myShape[1]->SetShapeLoc(210,110,290,190);
- myShape[2]->SetShapeLoc(220,120,280,180);
- myShape[3]->SetShapeLoc(230,130,270,170);
-
- /* place the line */
- myShape[4]->SetShapeLoc(100,50,300,50);
-
- /* place the ovals */
- myShape[5]->SetShapeLoc(100,100,200,200);
- myShape[6]->SetShapeLoc(110,110,190,190);
- myShape[7]->SetShapeLoc(120,120,180,185);
- myShape[8]->SetShapeLoc(130,130,170,170);
-
- /* here's the fun part! send each shape */
- /* a Draw() message, generically */
- for (i=0; i<9; i++)
- myShape[i]->Draw();
-
- /* relocate the line and Erase() */
- myShape[4]->SetShapeLoc(210,110,290,110);
- myShape[4]->Erase();
-
- /* delete everything, again generically */
- for (i=0; i<9; i++)
- {
- delete(myShape[i]);
- }
- }
-
- /* end of listing - DrawMyStuff.c */
-